<?php
require_once (COMM_PATH.'db.inc.php');
/**
 * 后台管理类 
 * 关于后台操作数据库的方法
 * Enter description here ...
 * @author fancy
 *
 */
class BMS extends DBSQL {
	public $_BOOK = "book_info";
	public $_CATEGORY = "book_cate";
	public $_CERROR  ="book_correctError";
	public $_LWORD  = "book_leaveword";
	public $_LINK  = "book_links";
	public $_pagesize = 10;
	
	public function __construct(){
		parent::__construct();
	}
	
	
	/**
	 * 新增数据
	 * Enter description here ...
	 * @param unknown_type $name 表名称
	 * @param unknown_type $data key表字段  val值     数组
	 */
	public function insertData($name,$data){
		$field = implode(',', array_keys($data));
		$int = 0;
		foreach ($data as $key=>$val){
			$value .= "'".$val."'";
			if($int<count($data)-1){
				$value .=",";
			}
			$int++;
		}
		
		$sql = "INSERT INTO ".$name."(".$field.") values (".$value.")";
		error_log("insert-error-msg :".$sql."\n",3,ROOT_PATH."/tmp/php_sql_err.log");
		return $this->insert($sql);
	}
	
	/**
	 * 删除一条记录 根据表名 和id
	 * Enter description here ...
	 * @param unknown_type $id
	 */
	public function delData($name,$id) {
		$sql = "DELETE FROM ".$name." WHERE BID = ".$id;
		return $this->delete($sql);
	}
	
	/**
	 * 更新指定表指定ID的调查表记录
	 * Enter description here ...
	 * @param unknown_type $name
	 * @param unknown_type $id
	 * @param unknown_type $data['字段名']=值
	 */
	public function updateData($name,$id,$data){
		$col = array();
		foreach ($data as $key => $value){
			$col[] = $key."='".$value."'";
		}
		$sql = "UPDATE ".$name." SET ".implode(',', $col)." WHERE BID = ".$id;
		return $this->update($sql);
	}
	
	
	/**
	 * 根据id查询表
	 * Enter description here ...
	 * @param unknown_type $id 主键id
	 * @param unknown_type $name 表名称
	 */
	public function getInfo($id,$name){
		$sql = "SELECT * FROM ".$name." WHERE BID = ".$id;
		return $this->select($sql);
	}
	
	/**
	 * 获取所有的分类
	 * Enter description here ...
	 */
	public function getAllCategory(){
		$sql = "SELECT * FROM ".$this->_CATEGORY;
		return $this->select($sql);
	}
	
	
	
	/**
	 * 分类管理 分页查询
	 * Enter description here ...
	 * @param unknown_type $pages
	 */
	public function getCateForPage($pages=1,$name="") {
		$start = ($pages-1)*$this->_pagesize;
		$sql = "SELECT * FROM ".$this->_CATEGORY;
		if ($name!=""){
			$sql .=" WHERE cate_name  like '%".$name."%'";
		}
		$sql .=" LIMIT $start , $this->_pagesize";
		return $this->select($sql);
	}
	
	/**
	 * 分类管理  查询分类总数
	 * Enter description here ...
	 * @param unknown_type $name
	 */
	public function getCateNum($name="") {
		$sql = "SELECT COUNT(BID) FROM ".$this->_CATEGORY;
		if ($name!=""){
			$sql .=" WHERE cate_name  like '*".$name."*'";
		}
		$num = $this->select($sql);
		return $num[0][0];
	}
	
	/**
	 * 连接管理 分页查询
	 * Enter description here ...
	 * @param unknown_type $pages
	 */
	public function getLinksForPage($pages=1,$name="") {
		$start = ($pages-1)*$this->_pagesize;
		$sql = "SELECT * FROM ".$this->_LINK;
		if ($name!=""){
			$sql .=" WHERE LINK_NAME  like '%".$name."%'";
		}
		$sql .=" LIMIT $start , $this->_pagesize";
		return $this->select($sql);
	}
	
	/**
	 * 分类管理  查询分类总数
	 * Enter description here ...
	 * @param unknown_type $name
	 */
	public function getLinksNum($name="") {
		$sql = "SELECT COUNT(BID) FROM ".$this->_LINK;
		if ($name!=""){
			$sql .=" WHERE LINK_NAME  like '*".$name."*'";
		}
		$num = $this->select($sql);
		return $num[0][0];
	}
	
	
	/**
	 * 留言管理 
	 * Enter description here ...
	 * @param unknown_type $pages
	 * @param unknown_type $name
	 */
	public function getLeaveWordForPage($pages=1,$name="") {
		$start = ($pages-1)*$this->_pagesize;
		$sql = "SELECT * FROM ".$this->_LWORD;
		if ($name!=""){
			$sql .=" WHERE username  like '%".$name."%'";
		}
		$sql .=" LIMIT $start , $this->_pagesize";
		return $this->select($sql);
	}
	
	/**
	 * 留言管理 查询数据
	 * Enter description here ...
	 * @param unknown_type $name
	 */
	public function getLeaveWordNum($name="") {
		$sql = "SELECT COUNT(BID) FROM ".$this->_LWORD;
		if ($name!=""){
			$sql .=" WHERE username  like '*".$name."*'";
		}
		$num = $this->select($sql);
		return $num[0][0];
	}
	
	/**
	 * 书籍管理 列表
	 * Enter description here ...
	 * @param unknown_type $pages
	 * @param unknown_type $bookname
	 * @param unknown_type $author
	 */
	public function getbookList($pages=1,$bookname="",$author="",$cateId=""){
		$start = ($pages-1)*$this->_pagesize;
		$sql = "SELECT * FROM ".$this->_BOOK." WHERE 1 = 1 ";
		if ($bookname!=""){
			$sql .=" AND bookName  like '%".$bookname."%'";
		}
		if ($author!=""){
			$sql .=" AND author  like '%".$author."%'";
		}
		if ($cateId!=""){
			$sql .=" AND category  = ".$cateId;
		}
		$sql .= " order by createTime desc";
		$sql .=" LIMIT $start , $this->_pagesize";
		return $this->select($sql);
	}
	
	/**
	 * 书籍管理 数量
	 * Enter description here ...
	 * @param unknown_type $bookname
	 * @param unknown_type $author
	 */
	public function getbookNum($bookname="",$author="",$cateId=""){
		$sql = "SELECT COUNT(BID) FROM ".$this->_BOOK." WHERE 1 = 1 ";
		if ($bookname!=""){
			$sql .=" AND bookName  like '%".$bookname."%'";
		}
		if ($author!=""){
			$sql .=" AND author  like '%".$author."%'";
		}
		if ($cateId!=""){
			$sql .=" AND category  = ".$cateId;
		}
		$num = $this->select($sql);
		return $num[0][0];
	}
	
	
	
}

?>